package com.hanter.android.radui.mvvm

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.IdRes
import androidx.lifecycle.Observer
import com.hanter.android.radui.R
import com.hanter.android.radui.mvp.BaseFragment
import com.hanter.android.radui.mvp.ContentLoadStatusViewHelper
import com.hanter.android.radui.mvp.LoadingDialogHelper

abstract class BaseMvvmFragment : BaseFragment(), View.OnClickListener {

    internal var contentView: View? = null
    private lateinit var contentStatusViewHelper: ContentLoadStatusViewHelper
    private lateinit var loadingDialogHelper: LoadingDialogHelper

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        this.contentView = super.onCreateView(inflater, container, savedInstanceState)
        val viewModel = initViewModel()
        initBaseView(viewModel)
        return this.contentView
    }

    private fun <T : View> findViewById(@IdRes id: Int): T? {
        return this.contentView?.findViewById(id)
    }

    abstract fun initViewModel(): BaseViewModel

    protected fun initBaseView(viewModel: BaseViewModel) {
        val contentView = findViewById<ViewGroup>(R.id.base_content_container)
        val realContentView = findViewById<View>(R.id.real_content_view)
        contentStatusViewHelper = ContentLoadStatusViewHelper(this, contentView, realContentView)

        loadingDialogHelper = LoadingDialogHelper(childFragmentManager)

        viewModel.showProgress.observe(this, Observer<Boolean> { t ->
            if (t) {
                loadingDialogHelper.showProgressDialog()
            } else {
                loadingDialogHelper.dismissProgressDialog()
            }
        })

        viewModel.pageState.observe(this, Observer<Int> { state ->
            when (state) {
                BaseViewModel.PAGE_STATE_LOADING -> {
                    contentStatusViewHelper.showLoadingView()
                }

                BaseViewModel.PAGE_STATE_CONTENT -> {
                    contentStatusViewHelper.showContentView()
                }

                BaseViewModel.PAGE_STATE_EXCEPTION -> {
                    contentStatusViewHelper.showExceptionView("", "")
                }

                BaseViewModel.PAGE_STATE_BACK -> {
                    activity?.onBackPressed()
                }
            }
        })

        viewModel.toast.observe(this, object : Observer<String?> {
            override fun onChanged(t: String?) {
                showToast(t)
            }
        })
    }

    override fun onDestroy() {
        if (::contentStatusViewHelper.isInitialized) {
            loadingDialogHelper.dismissProgressDialog()
        }
        super.onDestroy()
    }

    fun showToast(t: String?) {
        Toast.makeText(context, t, Toast.LENGTH_SHORT).show()
    }

    override fun onClick(v: View?) {

    }

}
